﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Common;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.Channel_Enum;
using AspNetCs.Bll;
using AspNetCs.Model;
using System.Data;
public partial class Master_Admin_TempleAdd : Admin_BasePage
{
    /// <summary>
    /// 模板添加的类型名称
    /// </summary>
    protected string TempleTypeName = "";
    /// <summary>
    /// 模板添加类型
    /// </summary>
    protected int TempleTypeId = 0;
    protected Cutter cut = new Cutter();
    protected Temple_Model model = new Temple_Model();
    protected string Action = "";
    protected int id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        IsExit();

        if (Request["channelId"] != null)
        {
            TempleTypeId = StringHelper.isNumber_int(Request["channelId"].ToString());
        }

        if (Request["id"] != null)
        {
            id = StringHelper.isNumber_int(Request["id"].ToString());
        }


        if (!Enum.IsDefined(typeof(Channel_Type), TempleTypeId))
        {
            ShowMsgBack("频道参数错误!");
            Response.End();
        }
        if (Request["Action"] != null)
        {
            Action = Request["Action"].ToString();
        }



        if (!Page.IsPostBack)
        {

            switch ((Channel_Type)TempleTypeId)
            {
                case Channel_Type.cemetery:
                    break;
                case Channel_Type.clan:



                    break;

                case Channel_Type.gravestone:
                    EnumDescription[] enumArry = EnumDescription.GetFieldTexts(typeof(NetMuTypeEnum));
                    this.DD_Types.DataSource = enumArry;
                    this.DD_Types.DataTextField = "EnumValueStr";
                    this.DD_Types.DataValueField = "enumRank";
                    this.DD_Types.DataBind();

                    break;
                case Channel_Type.memorial:
                    #region 绑定模板类型
                    DataTable dt = Memorial_TypeBll.CreateNew.GetDataTable("");

                    this.DD_Types.DataTextField = "TypeName";
                    this.DD_Types.DataValueField = "ID";
                    this.DD_Types.DataSource = dt;
                    this.DD_Types.DataBind();

                    #endregion
                    break;
            }

            if (Action == "Modify")
            {
                ModifyInfo();
            }
        }

        switch ((Channel_Type)TempleTypeId)
        {
            case Channel_Type.cemetery:
                TempleTypeName = EnumDescription.GetFieldText(Channel_Type.cemetery);
                cut.UpFoler = Admin_SkinFolder(Channel_Type.cemetery);
                this.Panel1.Visible = false;
                break;
            case Channel_Type.clan:
                TempleTypeName = EnumDescription.GetFieldText(Channel_Type.clan);
                cut.UpFoler = Admin_SkinFolder(Channel_Type.clan);
                this.Panel1.Visible = false;
                break;

            case Channel_Type.gravestone:
                TempleTypeName = EnumDescription.GetFieldText(Channel_Type.gravestone);
                cut.UpFoler = Admin_SkinFolder(Channel_Type.gravestone);
                this.Panel1.Visible = true;
                break;
            case Channel_Type.memorial:
                TempleTypeName = EnumDescription.GetFieldText(Channel_Type.memorial);
                cut.UpFoler = Admin_SkinFolder(Channel_Type.memorial);
                this.Panel1.Visible = true;
                break;
            case Channel_Type.FindFamily:
                TempleTypeName = EnumDescription.GetFieldText(Channel_Type.FindFamily);
                cut.UpFoler = Admin_SkinFolder(Channel_Type.FindFamily);
                this.Panel1.Visible = false;
                break;
        }

        cut.UpSize = configInfo.UpSize;
        cut.IsNewFile = true;
    }
    /// <summary>
    /// 修改赋值
    /// </summary>
    protected void ModifyInfo()
    {
        this.Button1.Visible = true;
        this.Button2.Visible = false;
        if (id <= 0)
        {
            ShowMsgBack("ID不能为空!");
            Response.End();
        }
        model = TempleBll.CreateNew.GetModel(id);
        this.Txt_TempleName.Text = model.TempleName;
        this.Txt_Path.Text = model.TemplePath;
        this.FileUpload1.Visible = false;
        this.Txt_InputUrl.Visible = true;
        this.Txt_InputUrl.Text = model.Thumbimg;
        this.DropDownList1.SelectedIndex = 1;
        this.Txt_point.Text = model.Point.ToString("0.##");
        if (model.Types > 0)
        {
            try
            {
                if (this.DD_Types.Items.FindByValue(model.Types.ToString()) != null)
                {
                    this.DD_Types.Items.FindByValue(model.Types.ToString()).Selected = true;
                }
            }
            finally { }
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string Msg = "";
        cut.UpFoler = cut.UpFoler + "/" + StringHelper.RemoveHtml(Txt_Path.Text);
        cut.UpSize = configInfo.UpSize;
        if (DropDownList1.SelectedValue == "Up")
        {

            bool isok = UpLoad_picture.UpSave(FileUpload1, cut, out Msg);
            if (!isok)
            {
                ShowMsgBack(Msg);
                Response.End();
            }
        }
        else
        {
            Msg = StringHelper.RemoveHtml(Txt_InputUrl.Text.ToString());
        }


        model.TempleName = Txt_TempleName.Text;
        model.TemplePath = Txt_Path.Text;
        model.Point = StringHelper.isNumber_decimal(Txt_point.Text);
        model.ChannelID = TempleTypeId;
        model.Types = StringHelper.isNumber_int(DD_Types.SelectedValue.ToString());
        model.Thumbimg = Msg;
        string ErrMsg = "";
        int result = TempleBll.CreateNew.Add(model, out ErrMsg);
        if (result > 0)
        {
            ResultStr("Admin_TempleList.aspx?channelId=" + TempleTypeId, ErrMsg);
        }
        else
        {
            ShowMsgBack(ErrMsg);
        }

    }
    protected void Button1_Click1(object sender, EventArgs e)
    {

        string Msg = "";
        cut.UpFoler = cut.UpFoler + "/" + StringHelper.RemoveHtml(Txt_Path.Text);

        if (DropDownList1.SelectedValue == "Up")
        {

            bool isok = UpLoad_picture.UpSave(FileUpload1, cut, out Msg);
            if (!isok)
            {
                ShowMsgBack(Msg);
                Response.End();
            }
        }
        else
        {
            Msg = StringHelper.RemoveHtml(Txt_InputUrl.Text.ToString());
        }
        model.TempleName = Txt_TempleName.Text;
        model.TemplePath = Txt_Path.Text;
        model.Point = StringHelper.isNumber_decimal(Txt_point.Text);
        model.Types = StringHelper.isNumber_int(DD_Types.SelectedValue.ToString());
        model.Thumbimg = Msg;
        model.Id = id;
        string ErrMsg = "";
        int result = TempleBll.CreateNew.Update(model, out ErrMsg);
        if (result > 0)
        {
            ResultStr("Admin_TempleList.aspx?channelId=" + TempleTypeId, ErrMsg);
        }
        else
        {
            ShowMsgBack(ErrMsg);
        }


    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "Up")
        {
            RequiredFieldValidator3.ControlToValidate = "FileUpload1";
            RequiredFieldValidator3.ErrorMessage = "请上传模板缩略图地址";
            this.FileUpload1.Visible = true;
            this.Txt_InputUrl.Visible = false;

        }
        else
        {
            RequiredFieldValidator3.ControlToValidate = "Txt_InputUrl";
            RequiredFieldValidator3.ErrorMessage = "请输入模板缩略图地址";
            this.FileUpload1.Visible = false;
            this.Txt_InputUrl.Visible = true;
        }

    }
}
